home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / sozobon / termcap.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  14KB  |  595 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22. /* BUFSIZE is the initial size allocated for the buffer
  23.    for reading the termcap file.
  24.    It is not a limit.
  25.    Make it large normally for speed.
  26.    Make it variable when debugging, so can exercise
  27.    increasing the space dynamically.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <termcap.h>
  33. #include <unistd.h>
  34.  
  35. #ifndef BUFSIZE
  36. #ifdef DEBUG
  37. #define BUFSIZE bufsize
  38. size_t bufsize = 128;
  39. #else
  40. #define BUFSIZE 2048
  41. #endif
  42. #endif
  43.  
  44. static void
  45. memory_out()
  46. {
  47.     static char msg[] = "Out of memory.\n";
  48.     write(2, msg, strlen(msg));
  49.     exit(1);
  50. }
  51.  
  52. static void *
  53. xmalloc(size)
  54.     size_t size;
  55. {
  56.     register void *tem = malloc(size);
  57.  
  58.     if (!tem)
  59.     memory_out();
  60.     return tem;
  61. }
  62.  
  63. static void *
  64. xrealloc(ptr, size)
  65.     void *ptr;
  66.     size_t size;
  67. {
  68.     register void *tem = realloc(ptr, size);
  69.  
  70.     if (!tem)
  71.     memory_out();
  72.     return tem;
  73. }
  74.  
  75. /* Looking up capabilities in the entry already found */
  76.  
  77. /* The pointer to the data made by tgetent is left here
  78.    for tgetnum, tgetflag and tgetstr to find.  */
  79.  
  80. static char *term_entry;
  81.  
  82. static char *tgetst1();
  83.  
  84. /* This is the main subroutine that is used to search
  85.    an entry for a particular capability */
  86.  
  87. static char *
  88. find_capability(bp, cap)
  89.     register char *bp,
  90.        *cap;
  91. {
  92.     for (; *bp; bp++)
  93.     if (bp[0] == ':'
  94.         && bp[1] == cap[0]
  95.         && bp[2] == cap[1])
  96.         return &bp[4];
  97.     return 0;
  98. }
  99.  
  100. int
  101. tgetnum(cap)
  102.     char *cap;
  103. {
  104.     register char *ptr = find_capability(term_entry, cap);
  105.  
  106.     if (!ptr || ptr[-1] != '#')
  107.     return -1;
  108.     return atoi(ptr);
  109. }
  110.  
  111. int
  112. tgetflag(cap)
  113.     char *cap;
  114. {
  115.     register char *ptr = find_capability(term_entry, cap);
  116.  
  117.     return 0 != ptr && ptr[-1] == ':';
  118. }
  119.  
  120. /* Look up a string-valued capability `cap'.
  121.    If `area' is nonzero, it points to a pointer to a block in which
  122.    to store the string.  That pointer is advanced over the space used.
  123.    If `area' is zero, space is allocated with `malloc'.  */
  124.  
  125. char *
  126. tgetstr(cap, area)
  127.     char *cap;
  128.     char **area;
  129. {
  130.     register char *ptr = find_capability(term_entry, cap);
  131.  
  132.     if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  133.     return 0;
  134.     return tgetst1(ptr, area);
  135. }
  136.  
  137. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  138.    gives meaning of character following \, or a space if no special meaning.
  139.    Eight characters per line within the string.  */
  140.  
  141. static char esctab[]
  142. = " \007\010  \033\014 \
  143.       \012 \
  144.   \015 \011 \013 \
  145.         ";
  146.  
  147. /* Given a pointer to a string value inside a termcap entry (`ptr'),
  148.    copy the value and process \ and ^ abbreviations.
  149.    Copy into block that *area points to,
  150.    or to newly allocated storage if area is 0.  */
  151.  
  152. static char *
  153. tgetst1(ptr, area)
  154.     char *ptr;
  155.     char **area;
  156. {
  157.     register char *p,
  158.        *r;
  159.     register int c;
  160.     register int c1;
  161.     register size_t size;
  162.     char *ret;
  163.  
  164.     if (!ptr)
  165.     return 0;
  166.  
  167.     /* `ret' gets address of where to store the string */
  168.     if (!area) {
  169.     /* Compute size of block needed (may overestimate) */
  170.     p = ptr;
  171.     while ((c = *p++) && c != ':' && c != '\n');
  172.     ret = (char *) xmalloc(p - ptr + 1);
  173.     } else
  174.     ret = *area;
  175.  
  176.     /* Copy the string value, stopping at null or colon.  */
  177.     /* Also process ^ and \ abbreviations.  */
  178.     p = ptr;
  179.     r = ret;
  180.     while ((c = *p++) && c != ':' && c != '\n') {
  181.     if (c == '^')
  182.         c = *p++ & 037;
  183.     else if (c == '\\') {
  184.         c = *p++;
  185.         if (c >= '0' && c <= '7') {
  186.         c -= '0';
  187.         size = 0;
  188.  
  189.         while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7') {
  190.             c *= 8;
  191.             c += c1 - '0';
  192.             p++;
  193.         }
  194.         } else if (c >= 0100 && c < 0200) {
  195.         c1 = esctab[(c & ~040) - 0100];
  196.         if (c1 != ' ')
  197.             c = c1;
  198.         }
  199.     }
  200.     *r++ = c;
  201.     }
  202.     *r = 0;
  203.     /* Update *area */
  204.     if (area)
  205.     *area = r + 1;
  206.     return ret;
  207. }
  208.  
  209. /* Outputting a string with padding */
  210.  
  211. short ospeed;
  212. char PC;
  213.  
  214. /* Actual baud rate if positive; - baud rate / 100 if negative. */
  215.  
  216. static short speeds[] =
  217. {
  218.  0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  219.  -18, -24, -48, -96, -192, -384
  220. };
  221.  
  222. void
  223. tputs(string, nlines, outfun)
  224.     register char *string;
  225.     int nlines;
  226.     register int (*outfun)();
  227. {
  228.     register int padcount = 0;
  229.  
  230.     if (!string)
  231.     return;
  232.     while (*string >= '0' && *string <= '9') {
  233.     padcount += *string++ - '0';
  234.     padcount *= 10;
  235.     }
  236.     if (*string == '.') {
  237.     string++;
  238.     padcount += *string++ - '0';
  239.     }
  240.     if (*string == '*') {
  241.     string++;
  242.     padcount *= nlines;
  243.     }
  244.     while (*string)
  245.     (*outfun) (*string++);
  246.  
  247.     /* padcount is now in units of tenths of msec.  */
  248.     padcount *= speeds[ospeed];
  249.     padcount += 500;
  250.     padcount /= 1000;
  251.     if (speeds[ospeed] < 0)
  252.     padcount = -padcount;
  253.     else {
  254.     padcount += 50;
  255.     padcount /= 100;
  256.     }
  257.  
  258.     while (padcount-- > 0)
  259.     (*outfun) (PC);
  260. }
  261.  
  262. /* Finding the termcap entry in the termcap data base */
  263.  
  264. struct buffer {
  265.     char *beg;
  266.     size_t size;
  267.     char *ptr;
  268.     int ateof;
  269.     int full;
  270. };
  271.  
  272. /* Forward declarations of static functions */
  273.  
  274. static int scan_file();
  275. static char *gobble_line();
  276. static int compare_contin();
  277. static int name_match();
  278.  
  279. /* Find the termcap entry data for terminal type `name'
  280.    and store it in the block that `bp' points to.
  281.    Record its address for future use.
  282.  
  283.    If `bp' is zero, space is dynamically allocated.  */
  284. /* 4/19/92 sb -- as per Steve Yelvingon's advice, tgetent() has been changed
  285.    to return int instead of (char *). */
  286.  
  287. int
  288. tgetent(bp, name)
  289.     char *bp,
  290.        *name;
  291. {
  292.     register char *tem;
  293.     register int fd;
  294.     struct buffer buf;
  295.     register char *bp1;
  296.     char *bp2;
  297.     char *term;
  298.     size_t malloc_size = 0;
  299.     register int c;
  300.     char *tcenv;        /* TERMCAP value, if it contais :tc=.  */
  301.     char *indirect = 0;        /* Terminal type in :tc= in TERMCAP value.  */
  302.     int filep;
  303.  
  304.     tem = (char *) getenv("TERMCAP");
  305.     if (tem && *tem == 0)
  306.     tem = 0;
  307.  
  308.     filep = tem && (*tem == '/');
  309.  
  310.     /*
  311.      * If tem is non-null and starts with / (in the un*x case, that is), it
  312.      * is a file name to use instead of /etc/termcap. If it is non-null and
  313.      * does not start with /, it is the entry itself, but only if the name
  314.      * the caller requested matches the TERM variable.
  315.      */
  316.  
  317.     if (tem && !filep && !strcmp(name, getenv("TERM"))) {
  318.     indirect = tgetst1(find_capability(tem, "tc"), (char *) 0);
  319.     if (!indirect) {
  320.         if (!bp)
  321.         bp = tem;
  322.         else
  323.         strcpy(bp, tem);
  324.         goto ret;
  325.     } else {        /* we will need to read /etc/termcap */
  326.         tcenv = tem;
  327.         tem = 0;
  328.     }
  329.     } else
  330.     indirect = 0;
  331.  
  332.     if (!tem)
  333.     tem = "/etc/termcap";
  334.  
  335.     /* Here we know we must search a file and tem has its name.  */
  336.  
  337.     fd = open(tem, 0, 0);
  338.     if (fd < 0)
  339.     return -1;
  340.  
  341.     buf.size = BUFSIZE;
  342.     /* Add 1 to size to ensure room for terminating null.  */
  343.     buf.beg = (char *) xmalloc(buf.size + 1);
  344.     term = indirect ? indirect : name;
  345.  
  346.     if (!bp) {
  347.     malloc_size = indirect ? strlen(tcenv) + 1 : buf.size;
  348.     bp = (char *) xmalloc(malloc_size);
  349.     }
  350.     bp1 = bp;
  351.  
  352.     if (indirect) {        /* copy the data from the environment
  353.                  * variable */
  354.     strcpy(bp, tcenv);
  355.     bp1 += strlen(tcenv);
  356.     }
  357.     while (term) {
  358.     /* Scan file, reading it via buf, till find start of main entry */
  359.     if (scan_file(term, fd, &buf) == 0)
  360.         return 0;
  361.  
  362.     /* Free old `term' if appropriate.  */
  363.     if (term != name)
  364.         free(term);
  365.  
  366.     /* If `bp' is malloc'd by us, make sure it is big enough.  */
  367.     if (malloc_size) {
  368.         malloc_size = bp1 - bp + buf.size;
  369.         tem = (char *) xrealloc(bp, malloc_size);
  370.         bp1 += tem - bp;
  371.         bp = tem;
  372.     }
  373.     bp2 = bp1;
  374.  
  375.     /* Copy the line of the entry from buf into bp.  */
  376.     tem = buf.ptr;
  377.     while ((*bp1++ = c = *tem++) && c != '\n')
  378.         /* Drop out any \ newline sequence. */
  379.         if (c == '\\' && *tem == '\n') {
  380.         bp1--;
  381.         tem++;
  382.         }
  383.     *bp1 = 0;
  384.  
  385.     /* Does this entry refer to another terminal type's entry?  */
  386.     /* If something is found, copy it into heap and null-terminate it */
  387.     term = tgetst1(find_capability(bp2, "tc"), 0);
  388.     }
  389.  
  390.     close(fd);
  391.     free(buf.beg);
  392.  
  393.     if (malloc_size) {
  394.     bp = (char *) xrealloc(bp, bp1 - bp + 1);
  395.     }
  396. ret:
  397.     term_entry = bp;
  398.     if (malloc_size)
  399.     return bp;
  400.     return 1;
  401. }
  402.  
  403. /* Given file open on `fd' and buffer `bufp',
  404.    scan the file from the beginning until a line is found
  405.    that starts the entry for terminal type `string'.
  406.    Returns 1 if successful, with that line in `bufp',
  407.    or returns 0 if no entry found in the file.  */
  408.  
  409. static int
  410. scan_file(string, fd, bufp)
  411.     char *string;
  412.     int fd;
  413.     register struct buffer *bufp;
  414. {
  415.     register char *tem;
  416.     register char *end;
  417.  
  418.     bufp->ptr = bufp->beg;
  419.     bufp->full = 0;
  420.     bufp->ateof = 0;
  421.     *bufp->ptr = 0;
  422.  
  423.     lseek(fd, 0L, 0);
  424.  
  425.     while (!bufp->ateof) {
  426.     /* Read a line into the buffer */
  427.     end = 0;
  428.     do {
  429.         /*
  430.          * if it is continued, append another line to it, until a
  431.          * non-continued line ends
  432.          */
  433.         end = gobble_line(fd, bufp, end);
  434.     }
  435.     while (!bufp->ateof && end[-2] == '\\');
  436.  
  437.     if (*bufp->ptr != '#'
  438.         && name_match(bufp->ptr, string))
  439.         return 1;
  440.  
  441.     /* Discard the line just processed */
  442.     bufp->ptr = end;
  443.     }
  444.     return 0;
  445. }
  446.  
  447. /* Return nonzero if NAME is one of the names specified
  448.    by termcap entry LINE.  */
  449.  
  450. static int
  451. name_match(line, name)
  452.     char *line,
  453.        *name;
  454. {
  455.     register char *tem;
  456.  
  457.     if (!compare_contin(line, name))
  458.     return 1;
  459.     /* This line starts an entry.  Is it the right one?  */
  460.     for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  461.     if (*tem == '|' && !compare_contin(tem + 1, name))
  462.         return 1;
  463.  
  464.     return 0;
  465. }
  466.  
  467. static int
  468. compare_contin(str1, str2)
  469.     register char *str1,
  470.        *str2;
  471. {
  472.     register int c1,
  473.         c2;
  474.  
  475.     while (1) {
  476.     c1 = *str1++;
  477.     c2 = *str2++;
  478.     while (c1 == '\\' && *str1 == '\n') {
  479.         str1++;
  480.         while ((c1 = *str1++) == ' ' || c1 == '\t');
  481.     }
  482.     if (c2 == '\0') {    /* end of type being looked up */
  483.         if (c1 == '|' || c1 == ':')    /* If end of name in data base, */
  484.         return 0;    /* we win. */
  485.         else
  486.         return 1;
  487.     } else if (c1 != c2)
  488.         return 1;
  489.     }
  490. }
  491.  
  492. /* Make sure that the buffer <- `bufp' contains a full line
  493.    of the file open on `fd', starting at the place `bufp->ptr'
  494.    points to.  Can read more of the file, discard stuff before
  495.    `bufp->ptr', or make the buffer bigger.
  496.  
  497.    Returns the pointer to after the newline ending the line,
  498.    or to the end of the file, if there is no newline to end it.
  499.  
  500.    Can also merge on continuation lines.  If `append_end' is
  501.    nonzero, it points past the newline of a line that is
  502.    continued; we add another line onto it and regard the whole
  503.    thing as one line.  The caller decides when a line is continued.  */
  504.  
  505. static char *
  506. gobble_line(fd, bufp, append_end)
  507.     int fd;
  508.     register struct buffer *bufp;
  509.     char *append_end;
  510. {
  511.     register char *end;
  512.     register size_t nread;
  513.     register char *buf = bufp->beg;
  514.     register char *tem;
  515.  
  516.     if (append_end == 0)
  517.     append_end = bufp->ptr;
  518.  
  519.     for (;;) {
  520.     end = append_end;
  521.     while (*end && *end != '\n')
  522.         end++;
  523.     if (*end)
  524.         break;
  525.     if (bufp->ateof)
  526.         return buf + bufp->full;
  527.     if (bufp->ptr == buf) {
  528.         if (bufp->full == bufp->size) {
  529.         bufp->size *= 2;
  530.         /* Add 1 to size to ensure room for terminating null.  */
  531.         tem = (char *) xrealloc(buf, bufp->size + 1);
  532.         bufp->ptr = (bufp->ptr - buf) + tem;
  533.         append_end = (append_end - buf) + tem;
  534.         bufp->beg = buf = tem;
  535.         }
  536.     } else {
  537.         append_end -= bufp->ptr - buf;
  538.         bcopy(bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
  539.         bufp->ptr = buf;
  540.     }
  541.     if (!(nread = read(fd, buf + bufp->full, bufp->size - bufp->full)))
  542.         bufp->ateof = 1;
  543.     bufp->full += nread;
  544.     buf[bufp->full] = 0;
  545.     }
  546.     return end + 1;
  547. }
  548.  
  549. #ifdef TEST
  550.  
  551. main(argc, argv)
  552.     int argc;
  553.     char **argv;
  554. {
  555.     char *term;
  556.     char *buf;
  557.  
  558.     term = argv[1];
  559.     printf("TERM: %s\n", term);
  560.  
  561.     buf = (char *) tgetent((char *) 0, term);
  562.     if ((long) buf <= 0) {
  563.     printf("No entry.\n");
  564.     return 0;
  565.     }
  566.     printf("Entry: %s\n", buf);
  567.  
  568.     tprint("cm");
  569.     tprint("AL");
  570.  
  571.     printf("co: %d\n", tgetnum("co"));
  572.     printf("am: %d\n", tgetflag("am"));
  573. }
  574.  
  575. tprint(cap)
  576.     char *cap;
  577. {
  578.     char *x = tgetstr(cap, (char *) 0);
  579.     register char *y;
  580.  
  581.     printf("%s: ", cap);
  582.     if (x) {
  583.     for (y = x; *y; y++)
  584.         if (*y <= ' ' || *y == 0177)
  585.         printf("\\%0o", *y);
  586.         else
  587.         putchar(*y);
  588.     free(x);
  589.     } else
  590.     printf("none");
  591.     putchar('\n');
  592. }
  593.  
  594. #endif                /* TEST */
  595.